﻿@model DiscountModel

@using SmartStore.Core.Domain.Discounts;
@using Telerik.Web.Mvc.UI;
@using SmartStore.Web.Framework.UI;

@Html.ValidationSummary(false)
@Html.HiddenFor(model => model.Id)
@Html.SmartStore().TabStrip().Name("discount-edit").Style(TabsStyle.Material).Items(x =>
{
    x.Add().Text(T("Admin.Promotions.Discounts.Info").Text).Content(TabInfo()).Selected(true);
    if (Model.Id > 0)
    {
        x.Add().Text(T("Admin.Promotions.Discounts.History").Text).Content(TabHistory());
    }
    
    EngineContext.Current.Resolve<IEventPublisher>().Publish(new TabStripCreated(x, "discount-edit", this.Html, this.Model));
})

@helper TabInfo()
{
    <script type="text/javascript">
            $(document).ready(function () {
                $("#@Html.FieldIdFor(model => model.DiscountTypeId)").change(toggleDiscountType);
                $("#@Html.FieldIdFor(model => model.UsePercentage)").click(toggleUsePercentage);
                $("#@Html.FieldIdFor(model => model.DiscountLimitationId)").change(toggleLimitation);

                toggleDiscountType();
                toggleLimitation();
                toggleUsePercentage();
            });
            
            function toggleDiscountType() {
            	var selectedDiscountTypeId = $("#@Html.FieldIdFor(model => model.DiscountTypeId)").val();

                if (selectedDiscountTypeId == '@((int)DiscountType.AssignedToCategories)') {
                	$('#pnlAppliedToCategories').show();
                	$('#pnlAppliedToManufacturers').hide();
                    $('#pnlAppliedToProducts').hide();
                }
            	else if (selectedDiscountTypeId == '@((int)DiscountType.AssignedToManufacturers)') {
            		$('#pnlAppliedToCategories').hide();
            		$('#pnlAppliedToManufacturers').show();
            		$('#pnlAppliedToProducts').hide();
            	}
                else if (selectedDiscountTypeId == '@((int)DiscountType.AssignedToSkus)') {
                	$('#pnlAppliedToCategories').hide();
                	$('#pnlAppliedToManufacturers').hide();
                    $('#pnlAppliedToProducts').show();
                }
                else {
                	$('#pnlAppliedToCategories').hide();
                	$('#pnlAppliedToManufacturers').hide();
                    $('#pnlAppliedToProducts').hide();
                }
            }

            function toggleLimitation() {
            	var selectedDiscountLimitationId = $("#@Html.FieldIdFor(model => model.DiscountLimitationId)").val();

                if (selectedDiscountLimitationId == '@((int)DiscountLimitationType.NTimesOnly)' || selectedDiscountLimitationId == '@((int)DiscountLimitationType.NTimesPerCustomer)') {
                    //'N Times Only' or 'N Times Per Customer'
                    $('#pnlLimitationTimes').show();
                }
                else {
                    $('#pnlLimitationTimes').hide();
                }
            }

            function toggleUsePercentage() {
                if ($('#@Html.FieldIdFor(model => model.UsePercentage)').is(':checked')) {
                    $('#pnlDiscountPercentage').show();
                    $('#pnlDiscountAmount').hide();
                }
                else {
                    $('#pnlDiscountPercentage').hide();
                    $('#pnlDiscountAmount').show();
				}
			}
    </script>

    <table class="adminContent">
        <tr>
            <td class="adminTitle">
                @Html.SmartLabelFor(model => model.Name)
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.Name)
                @Html.ValidationMessageFor(model => model.Name)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.SmartLabelFor(model => model.DiscountTypeId)
            </td>
            <td class="adminData">
                @Html.DropDownListFor(model => model.DiscountTypeId, ((DiscountType)Model.DiscountTypeId).ToSelectList())
                @Html.ValidationMessageFor(model => model.DiscountTypeId)
            </td>
        </tr>
        <tr id="pnlAppliedToCategories">
            <td class="adminTitle">
                @Html.SmartLabelFor(model => model.AppliedToCategories)
            </td>
            <td class="adminData form-control-plaintext wide">
				@if (!Model.AppliedToCategories.Any())
				{
					<div class="muted">@T("Admin.Promotions.Discounts.NoObjectsAssigned")</div>
				}
                @Html.Raw(string.Join(" · ", Model.AppliedToCategories.Select(x => $"<a href='{Url.Action("Edit", "Category", new { id = x.Id })}'>{x.Name.HtmlEncode()}</a>")))
            </td>
        </tr>
		<tr id="pnlAppliedToManufacturers">
			<td class="adminTitle">
				@Html.SmartLabelFor(model => model.AppliedToManufacturers)
			</td>
			<td class="adminData form-control-plaintext wide">
				@if (!Model.AppliedToManufacturers.Any())
				{
					<div class="muted">@T("Admin.Promotions.Discounts.NoObjectsAssigned")</div>
				}
                @Html.Raw(string.Join(" · ", Model.AppliedToManufacturers.Select(x => $"<a href='{Url.Action("Edit", "Manufacturer", new { id = x.Id })}'>{x.Name.HtmlEncode()}</a>")))
			</td>
		</tr>
        <tr id="pnlAppliedToProducts">
            <td class="adminTitle">
                @Html.SmartLabelFor(model => model.AppliedToProducts)
            </td>
            <td class="adminData form-control-plaintext wide">
				@if (!Model.AppliedToProducts.Any())
				{
					<div class="muted">@T("Admin.Promotions.Discounts.NoObjectsAssigned")</div>
				}
                @Html.Raw(string.Join(" · ", Model.AppliedToProducts.Select(x => $"<a href='{Url.Action("Edit", "Product", new { id = x.Id })}'>{x.Name.HtmlEncode()}</a>")))
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.SmartLabelFor(model => model.UsePercentage)
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.UsePercentage)
                @Html.ValidationMessageFor(model => model.UsePercentage)
            </td>
        </tr>
        <tr id="pnlDiscountAmount">
            <td class="adminTitle">
                @Html.SmartLabelFor(model => model.DiscountAmount)
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.DiscountAmount, new { postfix = Model.PrimaryStoreCurrencyCode })
                @Html.ValidationMessageFor(model => model.DiscountAmount)
            </td>
        </tr>
        <tr id="pnlDiscountPercentage">
            <td class="adminTitle">
                @Html.SmartLabelFor(model => model.DiscountPercentage)
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.DiscountPercentage)
                @Html.ValidationMessageFor(model => model.DiscountPercentage)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.SmartLabelFor(model => model.StartDateUtc)
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.StartDateUtc)
                @Html.ValidationMessageFor(model => model.StartDateUtc)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.SmartLabelFor(model => model.EndDateUtc)
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.EndDateUtc)
                @Html.ValidationMessageFor(model => model.EndDateUtc)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.SmartLabelFor(model => model.RequiresCouponCode)
            </td>
            <td class="adminData">
				@Html.CheckBoxFor(model => model.RequiresCouponCode, new { data_toggler_for = "#pnlCouponCode" })
                @Html.ValidationMessageFor(model => model.RequiresCouponCode)
            </td>
        </tr>
        <tr id="pnlCouponCode">
            <td class="adminTitle">
                @Html.SmartLabelFor(model => model.CouponCode)
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.CouponCode)
                @Html.ValidationMessageFor(model => model.CouponCode)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.SmartLabelFor(model => model.DiscountLimitationId)
            </td>
            <td class="adminData">
                @Html.DropDownListFor(model => model.DiscountLimitationId, ((DiscountLimitationType)Model.DiscountLimitationId).ToSelectList())
                @Html.ValidationMessageFor(model => model.DiscountLimitationId)
            </td>
        </tr>
        <tr id="pnlLimitationTimes">
            <td class="adminTitle">
                @Html.SmartLabelFor(model => model.LimitationTimes)
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.LimitationTimes, new { postfix = T("Admin.Promotions.Discounts.Fields.LimitationTimes.Times").Text })
                @Html.ValidationMessageFor(model => model.LimitationTimes) 
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.SmartLabelFor(model => model.SelectedRuleSetIds)
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.SelectedRuleSetIds)
                @Html.ValidationMessageFor(model => model.SelectedRuleSetIds)
            </td>
        </tr>
    </table>
}

@helper TabHistory()
{ 
	@(Html.Telerik().Grid<DiscountModel.DiscountUsageHistoryModel>()
        .Name("usagehistory-grid")
        .DataKeys(x =>
        {
            x.Add(y => y.Id).RouteKey("Id");
            x.Add(y => y.DiscountId).RouteKey("discountId");
        })
        .Columns(columns =>
        {
            columns.Bound(x => x.OrderId)
                .Width(100)
                .Template(x => Html.ActionLink("{0} (#{1})".FormatCurrentUI(T("Admin.Common.View").Text, x.OrderId), "Edit", "Order", new { id = x.OrderId }, new { }))
                .ClientTemplate("<a href=\"" + @Url.Content("~/Admin/Order/Edit/") + "<#= OrderId #>\">" + T("Admin.Common.View").Text + " (#<#= OrderId #>)</a>");
            columns.Bound(x => x.CreatedOn).Width(200);
            columns.Command(commands =>
            {
                commands.Delete().Localize(T);
            }).Width(100).Title(T("Admin.Common.Delete").Text);
        })
        .Pageable(settings => settings.PageSize(Model.GridPageSize).Position(GridPagerPosition.Both))
        .DataBinding(dataBinding =>
        {
            dataBinding.Ajax().Select("UsageHistoryList", "Discount", new { discountId = Model.Id })
                .Delete("UsageHistoryDelete", "Discount");
        })
		.PreserveGridState()
        .EnableCustomBinding(true))
}